package com.airspyar;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

public class VisibilityDialog extends Dialog implements SeekBar.OnSeekBarChangeListener{
	
	private final int SEEKBAR_STEP=10;
	private final SeekBar seekBar;
	private final TextView textView;
	private ResultListener listener=null;
	private int currentVisibility;
	
	public VisibilityDialog(Context ctx, int currentVisibility){
		super(ctx);
		this.setContentView(R.layout.visibility);
		this.setCancelable(true);
		
		seekBar=(SeekBar)this.findViewById(R.id.seekBar1);
		seekBar.setMax((PlanesDataProvider.VISIBILITY_MAX-PlanesDataProvider.VISIBILITY_MIN)/SEEKBAR_STEP);
		this.currentVisibility=currentVisibility;
		seekBar.setProgress((currentVisibility-PlanesDataProvider.VISIBILITY_MIN)/SEEKBAR_STEP);
		seekBar.setOnSeekBarChangeListener(this);
		
		Button buttonOk=(Button)this.findViewById(R.id.dialog_ok);
		Button buttonCancel=(Button)this.findViewById(R.id.dialog_cancel);
		
		buttonOk.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if(listener!=null)
					listener.onVisibilityChanged(VisibilityDialog.this.currentVisibility);
				VisibilityDialog.this.dismiss();
			}
		});
		
		buttonCancel.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				VisibilityDialog.this.cancel();
			}
		});
		
		textView=(TextView)this.findViewById(R.id.visibilityTextView);
		
		updateText(currentVisibility);
	}

	public interface ResultListener{
		public void onVisibilityChanged(int newVisibility);
	}
	
	public void setResultListener(ResultListener l){
		this.listener=l;
	}

	private void updateText(int dist){
		textView.setText("Visibility: "+dist+"km");
	}
	
	@Override
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		this.currentVisibility=PlanesDataProvider.VISIBILITY_MIN+progress*SEEKBAR_STEP;
		updateText(this.currentVisibility);
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar){
		
	}
}
